package com.jinbo.footmessageworker.remark;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.MyApplication;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.turn.MoreTurnActivity;
import com.jinbo.footmessageworker.utils.LocalPreferencesHelper;
import com.jinbo.footmessageworker.view.TitleBarView;

public class RemarkActivity extends BaseActivity implements View.OnClickListener {
    public static final  int GET_SERVER= 1;
    public static final  int GET_CUSTOMER= 2;
    TextView  mServerModTv, mServerDelTv,mCustomerModTv, mCustomerDelTv
            ,mServerContentTv,mServerTimeTv,mCustomerContentTv,mCustomerTimeTv;
    View mServerRemarkLl,mCustomerRemarkLl;
    Button mServerAddBtn,mCustomerAddBtn;
    private LocalPreferencesHelper mSharedPreference;
    private String mServerRemarkStr,mCustomerRemarkStr,mServerTimeStr,mCustomerTimeStr;
    String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remark);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("起钟备注");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        initView();
        setListener();
    }

    void initView() {
        /*mCustomerRemarkTv = (TextView) findViewById(R.id.customer_edit);
        mServerRemarkTv = (TextView) findViewById(R.id.serve_edit);*/
        mServerAddBtn=(Button)findViewById(R.id.server_add);
        mServerRemarkLl=findViewById(R.id.server_remark);
        mServerModTv=(TextView) findViewById(R.id.server_modify);
        mServerDelTv=(TextView) findViewById(R.id.server_delete);

        mCustomerAddBtn=(Button)findViewById(R.id.customer_add);
        mCustomerRemarkLl=findViewById(R.id.customer_remark);
        mCustomerModTv=(TextView) findViewById(R.id.customer_modify);
        mCustomerDelTv=(TextView) findViewById(R.id.customer_delete);

        mServerContentTv=(TextView)findViewById(R.id.server_remark_content);
        mServerTimeTv=(TextView)findViewById(R.id.server_remark_time);

        mCustomerContentTv=(TextView)findViewById(R.id.customer_remark_content);
        mCustomerTimeTv=(TextView)findViewById(R.id.customer_remark_time);

        update();




    }

    void update()
    {

        mSharedPreference = MyApplication.local;//new LocalPreferencesHelper(this,LocalPreferencesHelper.DB_NAME);
        mServerRemarkStr=mSharedPreference.getString(user_id+LocalPreferencesHelper.REMARK_WORKER);
        mCustomerRemarkStr=mSharedPreference.getString(user_id+LocalPreferencesHelper.REMARK_CUSTOMER);
        if(!TextUtils.isEmpty(mServerRemarkStr))
        {
            mServerAddBtn.setVisibility(View.INVISIBLE);
            mServerRemarkLl.setVisibility(View.VISIBLE);
            String showStr;
            String[] typeStr =mServerRemarkStr.split("/");
            String str=mServerRemarkStr.substring(typeStr[0].length()+1, mServerRemarkStr.length()-1);
            if(typeStr[0].equals("-1"))
                showStr=str;
            else
            {
                showStr=str+"/"+typeStr[0];
            }
            mServerContentTv.setText(showStr);



            mServerTimeStr=mSharedPreference.getString(user_id+LocalPreferencesHelper.REMARK_TIME_WORKER);
            mServerTimeTv.setText(mServerTimeStr);
        }
        if(!TextUtils.isEmpty(mCustomerRemarkStr))
        {
            mCustomerAddBtn.setVisibility(View.INVISIBLE);
            mCustomerRemarkLl.setVisibility(View.VISIBLE);

            String showStr;
            String[] typeStr =mCustomerRemarkStr.split("/");
            String str=mCustomerRemarkStr.substring(typeStr[0].length()+1, mCustomerRemarkStr.length()-1);
            if(typeStr[0].equals("-1"))
                showStr=str;
            else
            {
                showStr=str+"/"+typeStr[0];
            }
            mCustomerContentTv.setText(showStr);

            mCustomerTimeStr=mSharedPreference.getString(user_id+LocalPreferencesHelper.REMARK_TIME_CUSTOMER);
            mCustomerTimeTv.setText(mCustomerTimeStr);
        }
    }
    void setListener() {

       /* mCustomerRemarkTv.setOnClickListener(this);
        mServerRemarkTv.setOnClickListener(this);*/
        mServerAddBtn.setOnClickListener(this);
        mServerModTv.setOnClickListener(this);
        mServerDelTv.setOnClickListener(this);

        mCustomerAddBtn.setOnClickListener(this);
        mCustomerModTv.setOnClickListener(this);
        mCustomerDelTv.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.server_add:
                SelectServeToolActivity.launch(this,GET_SERVER);
                break;
            case R.id.server_modify:
                SelectServeToolActivity.launch(this,GET_SERVER);
                break;
            case R.id.server_delete:
                mServerRemarkLl.setVisibility(View.GONE);
                mServerAddBtn.setVisibility(View.VISIBLE);
                mSharedPreference.del(user_id+LocalPreferencesHelper.REMARK_WORKER);
                break;


            case R.id.customer_add:
                SelectCustomerThingsActivity.launch(this,GET_CUSTOMER);
                break;
            case R.id.customer_modify:
                SelectCustomerThingsActivity.launch(this,GET_CUSTOMER);
                break;
            case R.id.customer_delete:
                mCustomerRemarkLl.setVisibility(View.GONE);
                mCustomerAddBtn.setVisibility(View.VISIBLE);
                mSharedPreference.del(user_id+LocalPreferencesHelper.REMARK_CUSTOMER);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //requestCode标示请求的标示   resultCode表示有数据
        if (resultCode == RESULT_OK) {
                update();
        }
    }
}
